package statisticsCollection;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
 * A simple gui used for question answer collection, upon any set of 1q 1a. Easy to
 * return a unique answer to that question. Possible applications for big test creations, or multiple
 * choice questions and for the user to determine the right answer upon insertion, without having to 
 * remember the material.
 * @author A Dark Inc,
 *
 */
public class StatisticalCollectorBeta {
	
	private Map<String, String> quest;
	private JTextField insertAnswer;
	private JTextField insertQuestion;
	private JButton insert;
	private JButton makeSearch;
	private JTextArea correctAnswer;
	private JTextField searchQuestion;
	private JFrame _mainFrame;
	private JPanel buildSet;
	private JPanel answers;
	private JPanel answerField;
	private JButton clearFields;
	private JButton startOver;
	private JLabel questions;
	private JButton overWrite;
	public StatisticalCollectorBeta(){
		quest = new HashMap<String, String>();
		_mainFrame = new JFrame("Statistical Collector");
		buildSet = new JPanel();
		answers = new JPanel();
		questions = new JLabel("# of Q Stored: ");
		insertQuestion = new JTextField(10);
		insertAnswer = new JTextField(10);
		insertQuestion.setText("");
		insertAnswer.setText("");
		insert = new JButton("Insert");
		overWrite = new JButton("Overwrite");
		buildSet.add(insertQuestion);
		buildSet.add(insertAnswer);
		buildSet.add(insert);
		insert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String q, a;
				q = insertQuestion.getText();
				a = insertAnswer.getText();
				boolean qT = false, aT = false;
				
				if(q !=null)
					qT = true;
				
				if(a !=null)
					aT = true;
				
				if(qT)
					if(aT){
				addAnswer(q,a);
				}
			}
			
		});
		
		buildSet.add(overWrite);
		overWrite.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String q,a;
				q = insertQuestion.getText();
				a = insertAnswer.getText();
				boolean qT = false, aT = false;
				
				if(q !=null)
					qT = true;
				
				if(a !=null)
					aT = true;
				
				if(qT)
					if(aT){
				overWrite(q,a);
				}}});
		
		searchQuestion = new JTextField(10);
		
		correctAnswer = new JTextArea();
		correctAnswer.setText("NO ANSWER YET!");
		correctAnswer.setAutoscrolls(true);
		makeSearch = new JButton("Get Answer");
		answers.add(searchQuestion);
		answerField = new JPanel();
		answerField.add(correctAnswer);
		searchQuestion.setText("");	
		correctAnswer.setText("no such answer yet!");
		answers.add(makeSearch);
		answers.add(questions);
		startOver = new JButton("Clear Data!");
		startOver.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int ans = JOptionPane.showConfirmDialog(null,null, "Erase All Data?", 0);
				switch(ans){
					case JOptionPane.YES_OPTION:
						quest.clear();
						questions.setText("# of Q Stored:" + Integer.toString(quest.size()));
						break;
					case JOptionPane.NO_OPTION:
					break;
				}
				}
		});
		answers.add(startOver);
		makeSearch.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String q = searchQuestion.getText();
				String a = "";
				if(q != null){
					a = searchAnswer(q);
					correctAnswer.setText("");
					correctAnswer.append(a);
					searchQuestion.setText("");
				}
				else
					searchQuestion.setText("");	
			}
		});
		clearFields = new JButton("Clear Input");
		clearFields.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				correctAnswer.setText("");
				searchQuestion.setText("");
				insertQuestion.setText("");
				insertAnswer.setText("");
			}
		});
		answerField.add(clearFields);
		_mainFrame.add(answerField, BorderLayout.CENTER);
		_mainFrame.add(buildSet, BorderLayout.SOUTH);
		_mainFrame.add(answers, BorderLayout.NORTH);
		
		_mainFrame.pack();
		_mainFrame.setSize(500, 500);
		_mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		_mainFrame.setVisible(true);
	}
	
	public String searchAnswer(String question){
		String ans = "";
		
		if(quest.containsKey(question))
			ans = quest.get(question);
		else{
			ans = "GUESS. QUESTION NOT FOUND";
		}
		
		return ans;
	}
	
	private void addAnswer(String question, String answer){
		
		if(quest.get(question) == null){
			quest.put(question, answer);
			insertQuestion.setText("");
			insertAnswer.setText("");
		}
		else{
			insertQuestion.setText("Question Exists");
			insertAnswer.setText("Already!!!!");
		}
		questions.setText("# of Q Stored: " + Integer.toString(quest.size()));
	}
	
	public static void main(String[] args){
		new StatisticalCollectorBeta();
	}
	
	private void overWrite(String question, String answer){
		
		if(quest.get(question) != null){
			quest.remove(question);
			quest.put(question,answer);
			insertQuestion.setText("overWritten!");
			insertAnswer.setText("successfully!");
		}else
		addAnswer(question, answer);	
		
	}
}
